﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;


namespace TInventory
{
    public partial class CustomerListingForm : Form
    {
        TIEntities dbContext;
        
        public CustomerListingForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonAddCustomer_Click(object sender, EventArgs e)
        {
            AddEditCustomer form = new AddEditCustomer();


            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void RefreshGrid()
        {
            try
            {
                //var listOfCustomers = (from sup in dbContext.Customers select sup).ToList<Customer>();
                var listOfCustomers = (from sup in dbContext.Customers select new { sup.ID, sup.CustomerCompanyName, sup.CustomerName, sup.CustomerPhoneNumber, sup.CustomerAddress, sup.City, sup.CustomerFax, sup.CustomerEmailAddress, sup.OtherInformation}).ToList();
                dataGridViewCustomer.DataSource = listOfCustomers;

                dataGridViewCustomer.Columns[0].Visible = false;

                //dataGridViewCustomer.Columns["ID"].HeaderCell.Value = "ddd";
                dataGridViewCustomer.Columns["CustomerCompanyName"].HeaderCell.Value = "Customer Name";
                dataGridViewCustomer.Columns["CustomerName"].HeaderCell.Value = "Contact Person Name";
                dataGridViewCustomer.Columns["CustomerPhoneNumber"].HeaderCell.Value = "Phone #";
                dataGridViewCustomer.Columns["CustomerFax"].HeaderCell.Value = "Customer Fax";
                dataGridViewCustomer.Columns["CustomerEmailAddress"].HeaderCell.Value = "Email Address";
                dataGridViewCustomer.Columns["CustomerAddress"].HeaderCell.Value = "Customer Address";
                dataGridViewCustomer.Columns["OtherInformation"].HeaderCell.Value = "Other Information";
                dataGridViewCustomer.Columns["City"].HeaderCell.Value = "City";



            }
            catch (Exception e1)
            {


            }
        }

        private void CustomerListingForm_Load(object sender, EventArgs e)
        {
            RefreshGrid();
            
        }

        private void buttonEditSelected_Click(object sender, EventArgs e)
        {
            AddEditCustomer form = new AddEditCustomer();

            form.selectedRecord = dataGridViewCustomer.SelectedRows[0];
            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void buttonDeleteSelected_Click(object sender, EventArgs e)
        {
            int id = int.Parse(dataGridViewCustomer.SelectedRows[0].Cells["ID"].Value.ToString());
            Customer sup = (from s in dbContext.Customers where s.ID == id select s).FirstOrDefault();
            dbContext.DeleteObject(sup);
            dbContext.SaveChanges();
            RefreshGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RefreshGrid();

        }

        private void buttonDeleteSelected_ControlRemoved(object sender, ControlEventArgs e)
        {

        }

      
    }
}
